package com.zy.fee.roadpark.app;

import android.content.Context;
import cn.jpush.android.api.JPushInterface;
import com.lzy.imagepicker.ImagePicker;
import com.spd.print.jx.impl.PrintImpl;
import com.yanzhenjie.nohttp.InitializationConfig;
import com.yanzhenjie.nohttp.Logger;
import com.yanzhenjie.nohttp.NoHttp;
import com.yanzhenjie.nohttp.OkHttpNetworkExecutor;
import com.zygk.library.util.GlideImageLoader;
import com.zygk.library.util.LibraryHelper;
import org.litepal.LitePalApplication;

/**
 * Created by wikison on 2016/6/2.
 */
public class AppApplication extends LitePalApplication {
  private static final String TAG = AppApplication.class.getSimpleName();

  private static AppApplication mAppApplication;
  private static Context mContext;
  private static AppApplication _instance;
  public LibraryHelper libraryHelper;
  private static ImagePicker imagePicker;
  private static PrintImpl sPrinterImpl;
  public static Boolean isConnection = false;
  public String parkTelephone= "";  //停车场客服电话

  public AppApplication() {
    _instance = this;
  }

  public static Context getContext() {
    return mContext;
  }

  @Override public void onCreate() {
    super.onCreate();
    mContext = this;
    mAppApplication = this;
    sPrinterImpl = new PrintImpl();

    libraryHelper = new LibraryHelper(this);

    initPush();
    initImagePicker();
    initNoHttp();
  }

  /**
   * 获取Application
   */
  public static synchronized AppApplication getApp() {
    return mAppApplication;
  }

  public static AppApplication instance() {
    if (_instance == null) {
      throw new IllegalStateException("Application not init!!!");
    }
    return _instance;
  }

  public static PrintImpl getPrinterImpl() {
    return sPrinterImpl;
  }

  private void initPush() {
    try {
      JPushInterface.setDebugMode(true);  // 设置开启日志,发布时请关闭日志
      JPushInterface.init(this);        // 初始化 JPush
    } catch (Exception e) {
    }
  }

  private void initImagePicker() {
    imagePicker = ImagePicker.getInstance();
    imagePicker.setImageLoader(new GlideImageLoader());   //设置图片加载器
  }

  public static ImagePicker getImagePicker() {
    return imagePicker;
  }

  private void initNoHttp() {
    InitializationConfig config = InitializationConfig.newBuilder(this)
        // 全局连接服务器超时时间，单位毫秒，默认10s。
        .connectionTimeout(20 * 1000)
        // 全局等待服务器响应超时时间，单位毫秒，默认10s。
        .readTimeout(20 * 1000).networkExecutor(new OkHttpNetworkExecutor()).build();

    NoHttp.initialize(config); // NoHttp默认初始化。
    Logger.setDebug(true); // 开启NoHttp调试模式。
    Logger.setTag("NoHttpSample"); // 设置NoHttp打印Log的TAG。
  }
}
